<template>
<!-- ref 要加在container身上 -->
  <div class="swiper-container" :ref="name">
    <div class="swiper-wrapper">
      <div class="swiper-slide" v-for="banner in list" :key="banner.id" >
        <img :src="banner.imgUrl" />
      </div>
    </div>
    <!-- 如果需要分页器 -->
    <div class="swiper-pagination"></div>

    <!-- 如果需要导航按钮 -->
    <div class="swiper-button-prev"></div>
    <div class="swiper-button-next"></div>
  </div>
</template>

<script>
// 导入css和js
import Swiper from 'swiper'
import 'swiper/css/swiper.min.css'
export default {
  name: 'Cauousel',
  props: ['list','name'],
  watch: {
    list: {
      immediate: true,
      handler() {
        // 页面解析完成
        this.$nextTick(() => {
          new Swiper(this.$refs[this.name], {
            // 左右轮播图间隔的距离
            spaceBetween: 10,
            // 第一张轮播图往第二张轮播图切换总共的时间
            speed: 1000,
            // 无限轮播 从第一张开始
            loop: true,
            autoplay: {
              // 一张轮播图延迟一秒
              delay: 1000,
              // 点击之后禁用轮播 默认为false
              disableOnInteraction: false,
            },
            pagination: {
              el: '.swiper-pagination',
              clickable: true,
            },
            // navigation 导航 左右按钮切换
            navigation: {
              nextEl: '.swiper-button-next',
              prevEl: '.swiper-button-prev',
            },
          })
        })
      },
    },
  },
}
</script>

<style lang="less" scoped></style>
